﻿using System;
using Phoenix;
using Phoenix.ActionResults;
using TodoApp.Data;
using TodoApp.Models;

namespace TodoApp.Features.Tasks
{
    public class TasksController : Controller
    {
        readonly ITaskRepository taskRepository;

        public TasksController(ITaskRepository taskRepository)
        {
            this.taskRepository = taskRepository;
        }

        public ActionResult Index(int id)
        {
            var tasks = taskRepository.GetTasks();

            return Page(new IndexViewModel(tasks));
        }

        public ActionResult Create()
        {
            return Page(new CreateViewModel());
        }

        public ActionResult Create(string title, DateTime? dueDate)
        {
            var todoTask = new TodoTask(title, dueDate);
            taskRepository.Add(todoTask);

            Context.CompletionSource.SetResult(new CreateTaskResult(true, todoTask));
            return Back();
        }
    }
}